package com.ayf.payment.game.api.controller;

import com.ayf.payment.game.api.dto.ChargeOrderResponseDTO;
import com.ayf.payment.game.api.dto.MerchantDTO;
import com.ayf.payment.game.api.dto.OrderInfoDTO;
import com.ayf.payment.game.api.dto.OrderPageDTO;
import com.ayf.payment.game.api.dto.criteria.OrderInfoCriteria;
import com.ayf.payment.game.api.entity.MerchantPermission;
import com.ayf.payment.game.api.entity.MerchantRole;
import com.ayf.payment.game.api.entity.MerchantRolePermission;
import com.ayf.payment.game.api.entity.MerchantSon;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.service.*;
import com.ayf.payment.game.api.type.CommonConstants;
import com.ayf.payment.game.api.utils.CommonResult;
import com.ayf.payment.game.api.utils.DateUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.swwx.charm.commons.lang.utils.BeanConvertUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * @Description: 子账号管理
 * @Author: zh-liang
 * @Date : 2020-05-25 15:05
 */
@RestController
@RequestMapping("/v/son")
public class MerchantSonController {

    @Autowired
    private MerchantPermissionService merchantPermissionService;
    @Autowired
    private MerchantRoleService merchantRoleService;
    @Autowired
    private MerchantRolePermissionService merchantRolePermissionService;
    @Autowired
    private MerchantSonService merchantSonService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询角色列表
     *
     * @param merchantDTO
     * @return
     */
    @PostMapping("/roleList")
    public CommonResult<List<MerchantRole>> selectRoleList(MerchantDTO merchantDTO) {
        MerchantRole merchantRole = new MerchantRole();
        merchantRole.setMerchantId(merchantDTO.getId());
        List<MerchantRole> list = merchantRoleService.list(merchantRole);
        return new CommonResult<List<MerchantRole>>(CommonConstants.RETURN_SUCCESS, "查询角色列表成功", list);
    }

    /**
     * 添加角色
     *
     * @param merchantDTO
     * @param merchantRole
     * @return
     */
    @PostMapping("/addRole")
    public CommonResult<Object> addRole(MerchantDTO merchantDTO, @RequestBody MerchantRole merchantRole) {
        merchantRole.setMerchantId(merchantDTO.getId());
        if (merchantRoleService.addRole(merchantRole)) {
            return new CommonResult<Object>(CommonConstants.RETURN_SUCCESS, "添加角色成功");
        }
        return new CommonResult<Object>(CommonConstants.RETURN_ERROR, "添加角色失败");
    }

    /**
     * 删除角色
     *
     * @param merchantRole
     * @return
     */
    @PostMapping("/deleteRole")
    public CommonResult<Object> deleteRole(@RequestBody MerchantRole merchantRole) {
        if (merchantRoleService.deleteRole(merchantRole)) {
            return new CommonResult<Object>(CommonConstants.RETURN_SUCCESS, "删除角色成功");
        }
        return new CommonResult<Object>(CommonConstants.RETURN_ERROR, "删除角色失败");
    }

    /**
     * 更新角色
     *
     * @param merchantRole
     * @return
     */
    @PostMapping("/updateRole")
    public CommonResult<Object> updateRole(@RequestBody MerchantRole merchantRole) {
        if (merchantRoleService.updateRole(merchantRole)) {
            return new CommonResult<Object>(CommonConstants.RETURN_SUCCESS, "更新成功");
        }
        return new CommonResult<Object>(CommonConstants.RETURN_ERROR, "更新失败");
    }

    /**
     * 查询权限列表
     *
     * @param merchantDTO
     * @return
     */
    @PostMapping("/permissionList")
    public CommonResult<HashMap<String, Object>> selectPermissionList(MerchantDTO merchantDTO) {
        MerchantRole merchantRole = new MerchantRole();
        merchantRole.setMerchantId(merchantDTO.getId());
        List<MerchantPermission> list = merchantPermissionService.list(1);
        List<MerchantPermission> list2 = merchantPermissionService.list(2);
        HashMap<String, Object> map = new HashMap<>(8);
        map.put("web", list);
        map.put("app", list2);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "查询权限列表成功", map);
    }

    /**
     * 查询角色权限列表
     *
     * @param merchantDTO
     * @return
     */
    @PostMapping("/rolePermissionList")
    public CommonResult<List<Integer>> selectRolePermissionList(MerchantDTO merchantDTO,
                                                                @RequestBody MerchantRolePermission merchantRolePermission) {
        if (merchantRolePermission.getRoleId() == null) {
            throw new ParameterException("角色id不能为空");
        }
        MerchantRole merchantRole = new MerchantRole();
        merchantRole.setMerchantId(merchantDTO.getId());
        List<Integer> list = merchantRolePermissionService.list(merchantRolePermission.getRoleId());
        return new CommonResult<List<Integer>>(CommonConstants.RETURN_SUCCESS, "查询角色权限列表成功", list);
    }

    /**
     * 用户角色授权
     *
     * @param merchantRole
     * @return
     */
    @PostMapping("/setRolePermission")
    public CommonResult<Object> setRolePermission(@RequestBody MerchantRole merchantRole) {
        if (merchantRolePermissionService.updateRolePermission(merchantRole)) {
            return new CommonResult<Object>(CommonConstants.RETURN_SUCCESS, "修改成功");
        }
        return new CommonResult<Object>(CommonConstants.RETURN_ERROR, "修改失败");
    }

    /**
     * 查询子账号列表
     *
     * @param merchantDTO
     * @return
     */
    @PostMapping("/sonList")
    public CommonResult<List<MerchantSon>> sonList(MerchantDTO merchantDTO) {
        MerchantSon merchantSon = new MerchantSon();
        merchantSon.setMerchantId(merchantDTO.getId());
        List<MerchantSon> list = merchantSonService.list(merchantSon);
        return new CommonResult<List<MerchantSon>>(CommonConstants.RETURN_SUCCESS, "查询子账号列表成功", list);
    }

    /**
     * 添加子账号
     *
     * @param merchantDTO
     * @param merchantSon
     * @return
     */
    @PostMapping("/addMerchantSon")
    public CommonResult<Object> addMerchantSon(MerchantDTO merchantDTO, @RequestBody MerchantSon merchantSon) {
        merchantSon.setMerchantId(merchantDTO.getId());
        if (merchantSonService.addSon(merchantSon)) {
            return new CommonResult<Object>(CommonConstants.RETURN_SUCCESS, "添加子账号成功");
        }
        return new CommonResult<Object>(CommonConstants.RETURN_ERROR, "添加子账号失败");
    }

    /**
     * 删除子账号
     *
     * @param merchantSon
     * @return
     */
    @PostMapping("/deleteMerchantSon")
    public CommonResult<Object> deleteMerchantSon(MerchantDTO merchantDTO, @RequestBody MerchantSon merchantSon) {
        if (merchantSonService.deleteSon(merchantSon)) {
            tokenService.deleteTokenByLoginId(merchantDTO.getId(), merchantSon.getId());
            return new CommonResult<Object>(CommonConstants.RETURN_SUCCESS, "删除子账号成功");
        }
        return new CommonResult<Object>(CommonConstants.RETURN_ERROR, "删除子账号失败");
    }

    /**
     * 更新子账号
     *
     * @param merchantSon
     * @return
     */
    @PostMapping("/updateMerchantSon")
    public CommonResult<Object> updateMerchantSon(@RequestBody MerchantSon merchantSon) {
        if (merchantSonService.updateSon(merchantSon)) {
            return new CommonResult<Object>(CommonConstants.RETURN_SUCCESS, "修改成功");
        }
        return new CommonResult<Object>(CommonConstants.RETURN_ERROR, "修改失败");
    }

    /**
     * 更新子账号密码
     *
     * @param merchantSon
     * @return
     */
    @PostMapping("/updateMerchantSonPassword")
    public CommonResult<Object> updateMerchantSonPassword(MerchantDTO merchantDTO, @RequestBody MerchantSon merchantSon) {
        if (merchantSon.getId() == null) {
            throw new ParameterException("子账号id不能为空");
        }
        tokenService.deleteTokenByLoginId(merchantDTO.getId(),merchantSon.getId());
        if (merchantSonService.updateSonPassword(merchantSon)) {
            return new CommonResult<Object>(CommonConstants.RETURN_SUCCESS, "密码修改成功");
        }
        return new CommonResult<Object>(CommonConstants.RETURN_ERROR, "密码修改失败");
    }

    /**
     * 商户查询推广员列表
     *
     * @param merchantDTO
     * @return
     */
    @PostMapping("/tuiGList")
    public CommonResult<List<MerchantSon>> tuiGList(MerchantDTO merchantDTO) {
        List<MerchantSon> merchantSons = merchantSonService.tuiGList(merchantDTO.getId());
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "查询成功", merchantSons);
    }

    /**
     * 商户查询推广员订单
     *
     * @param merchantDTO
     * @param criteria
     * @return
     */
    @PostMapping("/orderList")
    public CommonResult<OrderPageDTO> orderList(MerchantDTO merchantDTO, @RequestBody OrderInfoCriteria criteria) {
        criteria.setMerchantId(merchantDTO.getId());
        MerchantSon merchantSon = merchantSonService.selectInfo(merchantDTO.getLoginId());
        if (merchantSon != null && StringUtils.isNotBlank(merchantSon.getGroupId())) {
            criteria.setSonId(merchantDTO.getLoginId());
        }
        if (StringUtils.isNotBlank(criteria.getStartTime())) {
            String time = DateUtils.stringFormat(criteria.getStartTime(),
                    DateUtils.formatPattern_full, DateUtils.formatPattern_Short);
            if (StringUtils.isNotBlank(time)) {
                criteria.setOrderGenerateTimeIndexStart(Integer.valueOf(time));
            }
        }
        if (StringUtils.isNotBlank(criteria.getEndTime())) {
            String time = DateUtils.stringFormat(criteria.getEndTime(),
                    DateUtils.formatPattern_full, DateUtils.formatPattern_Short);
            if (StringUtils.isNotBlank(time)) {
                criteria.setOrderGenerateTimeIndexEnd(Integer.valueOf(time));
            }
        }
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<OrderInfoDTO> dto = orderService.selectSonOrderList(criteria);
        PageInfo<OrderInfoDTO> pageInfo = new PageInfo<>(dto);
        ChargeOrderResponseDTO chargeOrderResponseDTO = orderService.selectSumSonAmount(criteria);
        OrderPageDTO dto1 = BeanConvertUtils.convert(pageInfo, OrderPageDTO.class);
        if (merchantSon == null || StringUtils.isEmpty(merchantSon.getGroupId())) {
            dto1.setMerchantTotalIncome(chargeOrderResponseDTO.getMerchantTotalIncome());
        }
        dto1.setSuccessTotal(chargeOrderResponseDTO.getSuccessTotal());
        dto1.setSuccessTotalAmount(chargeOrderResponseDTO.getSuccessTotalAmount());
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "查询成功", dto1);
    }

    /**
     * 查询子账户信息
     *
     * @param merchantDTO
     * @return
     */
    @PostMapping("/selectOne")
    public CommonResult<MerchantSon> selectOne(MerchantDTO merchantDTO) {
        MerchantSon merchantSon = merchantSonService.selectInfo(merchantDTO.getLoginId());
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "查询成功", merchantSon);
    }
}
